<?php

require_once 'modules/product/models/ProductCategory.php';
require_once 'modules/content/models/Content.php';
require_once 'modules/content/models/ContentCategory.php';
require_once 'modules/ads/models/Ads.php';
require_once 'modules/product/models/Product.php';

class default_IndexController extends Nine_Controller_Action
{
	public function indexAction()
	{
		/**
		 * Display tempalte
		 */
	    $this->view->headTitle('Home');
	    $this->view->menuId = 'home';
	    
		/**
		 * Get content news
		 */
		$objContent = new Models_Content();
		$objContentCategory = new Models_ContentCategory();
		$objProduct = new Models_ProductCategory();
		
		$objPro = new Models_Product();
		
		$cat = array();
		
		//get about-us
		$gId = 47;
		$about = $objContent->getContentByGid($gId)->toArray();
		$about['title'] = Nine_Function::subStringAtBlank(trim(strip_tags($about['title'])),30);
		$about['url'] = Nine_Route::_("content/index/detail/id/$gId",array('alias' => $about['alias']));
		   	
		
		
		// get nha trang booking
		$gId1 = 61;
		$book = $objProduct->getProductCatById($gId1);
//		$book['description'] = Nine_Function::subStringAtBlank(trim(strip_tags($book['description'])), 150);
		$tmp = explode('||', $book['images']);
		$book['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 330, 146,null,true);
		$book['link'] = Nine_Route::_("product/index/index/cid/{$gId1}",array('alias' => $book['alias']));	
		
		
		// get tour
		$gId2 = 63;
		$tour = $objProduct->getProductCatById($gId2);
//		$tour['description'] = Nine_Function::subStringAtBlank(trim(strip_tags($tour['description'])), 150);
		$tmp = explode('||', $tour['images']);
		$tour['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 330, 146,null,true);
		$tour['link'] = Nine_Route::_("product/index/index/cid/{$gId2}",array('alias' => $tour['alias']));	
			
		$categoryGid = 65;		
		$allHotels = $objPro->getAllEnabledProductsByCategory($categoryGid,array('sorting ASC','product_id DESC'));
		$i = 0;
		$hotel = array();
		foreach ($allHotels as $item){
			if(2!=$i){
				$item['created_date'] = date(Nine_Registry::getConfig('dateFormat'),$item['created_date']);
	        	if (null != $item['sale_off_start_date']) {
	        		$item['sale_off_start_date'] = date(Nine_Registry::getConfig('dateFormat'),@$item['sale_off_start_date']);
	        	}
	        	if (null != $item['sale_off_end_date']){
	        		$item['sale_off_end_date'] = date(Nine_Registry::getConfig('dateFormat'),@$item['sale_off_end_date']);
	        	}
				$item['main_title'] = $item['title'];
	        	$item['title'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['title'])),30);
	        	
	        	if(null!=$item['intro_text']){
        		$item['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['intro_text'])), 145);
        		}
       			 else{
        		$item['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['intro_text'])), 145);
        		}    
	        	$item['link'] = Nine_Route::_("product/index/detail/id/{$item['product_gid']}",array('alias' => $item['alias']));
	        	
	        	if (null != $item['images']) {
	        		$item['images'] = explode("||", $item['images']);
	        		$item['main_image'] = Nine_Function::getThumbImage(@$item['images'][0], 141, 98,null,true);
	        	}
	        	else {
	        		$item['main_image'] = Nine_Function::getThumbImage("media/userfiles/images/noimage.jpg",141,98,null,true);
	        	}
	        	$item['price'] = $objPro->makeUpPrice($item['price']);
        	
				$hotel[] = $item;
				$i++;
			}
			else{
				break;
			}
		}
		/**
		 * Assign to view
		 */
	    $this->view->hotel = $hotel;
		$this->view->tour = $tour;
		$this->view->book = $book;
		$this->view->cat = $cat;
		$this->view->about = $about;
		
		
	    $isLogin = $this->auth->isLogin();
	}
}